const Router = require('@koa/router');
const paymentController = require('../controllers/payment.controller');
const { checkAuth } = require('../middlewares/auth');

const router = new Router({
  prefix: '/api/v1/payments'
});

/**
 * @swagger
 * /api/v1/payments:
 *   get:
 *     tags: [支付管理]
 *     summary: 获取支付记录列表
 *     description: 获取当前用户的所有支付记录（支持分页和筛选）
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: query
 *         name: page
 *         schema:
 *           type: integer
 *           minimum: 1
 *           default: 1
 *         description: 页码
 *       - in: query
 *         name: limit
 *         schema:
 *           type: integer
 *           minimum: 1
 *           maximum: 100
 *           default: 10
 *         description: 每页数量
 *       - in: query
 *         name: status
 *         schema:
 *           type: string
 *           enum: [pending, processing, success, failed, refunded]
 *         description: 支付状态筛选
 *       - in: query
 *         name: method
 *         schema:
 *           type: string
 *           enum: [wechat, alipay, bank, points]
 *         description: 支付方式筛选
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 total:
 *                   type: integer
 *                   example: 100
 *                 pages:
 *                   type: integer
 *                   example: 10
 *                 current:
 *                   type: integer
 *                   example: 1
 *                 payments:
 *                   type: array
 *                   items:
 *                     type: object
 *                     properties:
 *                       id:
 *                         type: string
 *                         example: "pay_001"
 *                       orderId:
 *                         type: string
 *                         example: "order_001"
 *                       amount:
 *                         type: number
 *                         example: 99.99
 *                       method:
 *                         type: string
 *                         example: "wechat"
 *                       status:
 *                         type: string
 *                         example: "success"
 *                       transactionId:
 *                         type: string
 *                         example: "wx202401150001"
 *                       createdAt:
 *                         type: string
 *                         format: date-time
 *       401:
 *         description: 未认证
 */
router.get('/', checkAuth, paymentController.getPayments);

/**
 * @swagger
 * /api/v1/payments/{id}:
 *   get:
 *     tags: [支付管理]
 *     summary: 获取支付记录详情
 *     description: 获取指定支付记录的详细信息
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付记录ID
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "pay_001"
 *                 orderId:
 *                   type: string
 *                   example: "order_001"
 *                 amount:
 *                   type: number
 *                   example: 99.99
 *                 method:
 *                   type: string
 *                   example: "wechat"
 *                 status:
 *                   type: string
 *                   example: "success"
 *                 transactionId:
 *                   type: string
 *                   example: "wx202401150001"
 *                 metadata:
 *                   type: object
 *                   description: 支付相关的元数据
 *                 errorMessage:
 *                   type: string
 *                   description: 支付失败时的错误信息
 *                 successTime:
 *                   type: string
 *                   format: date-time
 *                   description: 支付成功时间
 *                 refundInfo:
 *                   type: object
 *                   properties:
 *                     refundId:
 *                       type: string
 *                       example: "ref_001"
 *                     refundAmount:
 *                       type: number
 *                       example: 99.99
 *                     refundTime:
 *                       type: string
 *                       format: date-time
 *                     refundReason:
 *                       type: string
 *                       example: "商品缺货"
 *                 createdAt:
 *                   type: string
 *                   format: date-time
 *                 updatedAt:
 *                   type: string
 *                   format: date-time
 *       401:
 *         description: 未认证
 *       404:
 *         description: 支付记录不存在
 */
router.get('/:id', checkAuth, paymentController.getPayment);

/**
 * @swagger
 * /api/v1/payments:
 *   post:
 *     tags: [支付管理]
 *     summary: 创建支付记录
 *     description: 为订单创建新的支付记录
 *     security:
 *       - bearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - orderId
 *               - amount
 *               - method
 *             properties:
 *               orderId:
 *                 type: string
 *                 description: 关联的订单ID
 *                 example: "order_001"
 *               amount:
 *                 type: number
 *                 description: 支付金额
 *                 example: 99.99
 *               method:
 *                 type: string
 *                 enum: [wechat, alipay, bank, points]
 *                 description: 支付方式
 *                 example: "wechat"
 *               metadata:
 *                 type: object
 *                 description: 支付相关的额外信息
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 id:
 *                   type: string
 *                   example: "pay_001"
 *                 paymentUrl:
 *                   type: string
 *                   description: 支付链接（微信/支付宝）
 *                   example: "https://wx.tenpay.com/pay.html"
 *                 qrCode:
 *                   type: string
 *                   description: 支付二维码（Base64）
 *                 message:
 *                   type: string
 *                   example: "支付创建成功"
 *       400:
 *         description: 参数错误
 *       401:
 *         description: 未认证
 *       404:
 *         description: 订单不存在
 */
router.post('/', checkAuth, paymentController.createPayment);

/**
 * @swagger
 * /api/v1/payments/{id}/process:
 *   put:
 *     tags: [支付管理]
 *     summary: 处理支付
 *     description: 处理支付记录的状态变更
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - status
 *             properties:
 *               status:
 *                 type: string
 *                 enum: [processing, success, failed]
 *                 description: 新的支付状态
 *                 example: "success"
 *               errorMessage:
 *                 type: string
 *                 description: 支付失败时的错误信息
 *               transactionId:
 *                 type: string
 *                 description: 支付平台的交易号
 *                 example: "wx202401150001"
 *     responses:
 *       200:
 *         description: 处理成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "支付状态更新成功"
 *       400:
 *         description: 参数错误或状态变更无效
 *       401:
 *         description: 未认证
 *       404:
 *         description: 支付记录不存在
 */
router.put('/:id/process', checkAuth, paymentController.processPayment);

/**
 * @swagger
 * /api/v1/payments/{id}/refund:
 *   post:
 *     tags: [支付管理]
 *     summary: 申请退款
 *     description: 为已支付的记录申请退款
 *     security:
 *       - bearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: string
 *         description: 支付记录ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - amount
 *               - reason
 *             properties:
 *               amount:
 *                 type: number
 *                 description: 退款金额
 *                 example: 99.99
 *               reason:
 *                 type: string
 *                 description: 退款原因
 *                 example: "商品缺货"
 *     responses:
 *       200:
 *         description: 退款申请成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 refundId:
 *                   type: string
 *                   example: "ref_001"
 *                 message:
 *                   type: string
 *                   example: "退款申请已提交"
 *       400:
 *         description: 参数错误或不可退款
 *       401:
 *         description: 未认证
 *       404:
 *         description: 支付记录不存在
 */
router.post('/:id/refund', checkAuth, paymentController.refundPayment);

/**
 * @swagger
 * /api/v1/payments/notify/wechat/{appId}:
 *   post:
 *     tags: [支付管理]
 *     summary: 微信支付回调
 *     description: 处理微信支付的异步通知
 *     parameters:
 *       - in: path
 *         name: appId
 *         required: true
 *         schema:
 *           type: string
 *         description: 微信应用ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/xml:
 *           schema:
 *             type: object
 *     responses:
 *       200:
 *         description: 回调处理成功
 *         content:
 *           application/xml:
 *             schema:
 *               type: object
 *               properties:
 *                 return_code:
 *                   type: string
 *                   example: "SUCCESS"
 *       400:
 *         description: 回调数据验证失败
 */
router.post('/notify/wechat/:appId', paymentController.handleWechatNotify);

/**
 * @swagger
 * /api/v1/payments/notify/alipay:
 *   post:
 *     tags: [支付管理]
 *     summary: 支付宝回调
 *     description: 处理支付宝的异步通知
 *     requestBody:
 *       required: true
 *       content:
 *         application/x-www-form-urlencoded:
 *           schema:
 *             type: object
 *     responses:
 *       200:
 *         description: 回调处理成功
 *         content:
 *           text/plain:
 *             schema:
 *               type: string
 *               example: "success"
 *       400:
 *         description: 回调数据验证失败
 */
router.post('/notify/alipay', paymentController.handleAlipayNotify);

module.exports = router; 